﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using vppServer;
using System.Windows.Forms;

using PowerPoint = Microsoft.Office.Interop.PowerPoint;
using Microsoft.Office.Core;

using System.Runtime.InteropServices;

namespace PPTPlug
{
    public class PPTPlug
    {
        NotifyIcon TrayNotofier=new NotifyIcon();
        public event EventHandler OnConnectionRequestRecibed;
        VirtualBlankScreen.BlankScreen _blankscreen = new VirtualBlankScreen.BlankScreen();
        vppserver _server;
       
        WifiChannel.WifiChannel _wifi = new WifiChannel.WifiChannel();
        PowerPoint.Application objApp;
        PowerPoint.SlideShowWindow wnd { get; set; }
        PowerPoint.Presentations objPresSet;
        PowerPoint._Presentation objPres;
       
      
        PowerPoint.SlideShowSettings objSSS;
       
      
        private bool respond;
        private System.Diagnostics.Process proc;


        public void DoAction(int code)
        {
            try
            {
                if (respond)
                {
                    switch (code)
                    {
                        case 0x00:// Is Channel Valid
                          //  this.ShowNotoficationIcone(NotificationFlags.IDLE);
                            break;
                        case 0x01:// Start Channel
                            _server.SendCode("0x02");
                          //  this.ShowNotoficationIcone(NotificationFlags.CONNECTION);
                            break;
                        case 0x03:// Stop Channel
                          //  this.ShowNotoficationIcone(NotificationFlags.LOSTCONNECTION);
                            break;
                        case 0x05:// Start
                            wnd = objSSS.Run();
                            objApp.ActivePresentation.SlideShowWindow.View.First();
                         //   this.ShowNotoficationIcone(NotificationFlags.CONNECTION);
                            break;
                        case 0x06: // Stop
                            objApp.ActivePresentation.SlideShowWindow.View.Exit();
                         //   this.ShowNotoficationIcone(NotificationFlags.CONNECTION);
                            break;
                        case 0x07: // pause
                         //   this.ShowNotoficationIcone(NotificationFlags.CONNECTION);
                            ShowWaitScreen();

                            break;
                        case 0x09: // next
                            objApp.ActivePresentation.SlideShowWindow.View.Next();
                         //   this.ShowNotoficationIcone(NotificationFlags.CONNECTION);
                            break;
                        case 0x08: // prev
                            objApp.ActivePresentation.SlideShowWindow.View.Previous();
                          //  this.ShowNotoficationIcone(NotificationFlags.CONNECTION);
                            break;
                        case 0x0a: // last
                            objApp.ActivePresentation.SlideShowWindow.View.Last();
                            this.ShowNotoficationIcone(NotificationFlags.CONNECTION);
                            break;
                        case 0x0b: // first
                            objApp.ActivePresentation.SlideShowWindow.View.First();
                          //  this.ShowNotoficationIcone(NotificationFlags.CONNECTION);
                            break;
                        case 0x0c: // halt
                            HideWaitScreen();
                          //  this.ShowNotoficationIcone(NotificationFlags.CONNECTION);
                            break;
                        case 0x0d: // useWifi

                         //   this.ShowNotoficationIcone(NotificationFlags.CONNECTION);
                            break;

                        case 0x0e:// useBT
                            this.ShowNotoficationIcone(NotificationFlags.CONNECTION);
                           // this.ShowNotoficationIcone(NotificationFlags.BLUETOOTH);
                            break;

                        case 0x10:// Close Presntation
                            this.ReleasePowerPointAndClose();
                          //  this.ShowNotoficationIcone(NotificationFlags.LOSTCONNECTION);
                            break;


                    }
                }
            }
            catch (Exception err)
            {

                System.Windows.Forms.MessageBox.Show(err.Message);
                _server.CloseChannel();
            }

        }

        private void HideWaitScreen()
        {
            _blankscreen.Hide();
        }
        /// <summary>
        /// View the Weighting screen
        /// </summary>
        private void ShowWaitScreen()
        {
            _blankscreen.Show();
        }

        public void init(String fn)
        {
            respond = true;
            //Create a new presentation based on a template.
            objApp = new PowerPoint.Application();
           
            objPres = objApp.Presentations.Open(fn, MsoTriState.msoTrue, MsoTriState.msoTrue, MsoTriState.msoTrue);

            this.objSSS = objPres.SlideShowSettings;

            // Start the server to listine for requests
            _server = _wifi; 
            _server.Initalise();
            _server.OpenChannel();
            _server.OnMessgaRecived += new EventHandler<MessageRecivedEventArgs>(_server_OnMessgaRecived);
            _server.StartListning();
            
        }

        public void init(PowerPoint.Presentation objPres)
        {
            respond = true;
            objApp = objPres.Application;
            this.objPres = objPres;

            this.objSSS = objPres.SlideShowSettings;


            // Start the server to listine for requests
            _server = _wifi;
            _server.Initalise();
            _server.OpenChannel();
            _server.OnMessgaRecived += new EventHandler<MessageRecivedEventArgs>(_server_OnMessgaRecived);
            _server.StartListning();

         //   this.ShowNotoficationIcone(NotificationFlags.CONNECTION);

          
        }

        private void initalize()
        {
           
        }
        /// <summary>
        /// Perform Message 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void _server_OnMessgaRecived(object sender, MessageRecivedEventArgs e)
        {
             String m = e.Code;
              int value = Convert.ToInt32(m, 16);
              DoAction(value);
        }
        public void ReleasePowerPointAndClose()
        {
        //String presentationTitle = objPres.Name;
            Microsoft.Office.Interop.PowerPoint.Slides slides = objPres.Slides;
        for (int i = 1; i <= slides.Count; i++)
        {
            Microsoft.Office.Interop.PowerPoint.Slide slide = slides[i];
            String slideName = slide.Name;
            releaseCOM(slide);
            objApp.Quit();
        }
        }

        private static void releaseCOM(object o)
        {

            try
            {
                System.Runtime.InteropServices.Marshal.FinalReleaseComObject(o);
            }
            catch { }
            finally
            {
                o = null;
            }
        }

        public void Close()
        {
            try
            {
                _server.CloseChannel();
            }
            catch (Exception err)
            {
            }
           
        }
        public void ShowNotfivationIcon()
        {
            TrayNotofier.BalloonTipText = "Server Start";
            TrayNotofier.Visible = true;
            TrayNotofier.ShowBalloonTip(5000);
        }

        public void ShowNotoficationIcone(NotificationFlags flags)
        {
            try
            {

                switch (flags)
                {
                    case NotificationFlags.IDLE:
                        TrayNotofier.Icon = new System.Drawing.Icon(System.IO.Directory.GetCurrentDirectory() + @"/images/notofy1_pbm_pp_vi_idle.ico");
                        break;
                    case NotificationFlags.CONNECTION:
                        TrayNotofier.Icon = new System.Drawing.Icon(System.IO.Directory.GetCurrentDirectory() + @"/images/notofy1_pbm_pp_vi.ico");
                        break;
                    case NotificationFlags.LOSTCONNECTION:
                        TrayNotofier.Icon = new System.Drawing.Icon(System.IO.Directory.GetCurrentDirectory() + @"/images/notofy1_pbm_pp_vi_nc.ico");
                        break;
                    case NotificationFlags.BLUETOOTH:
                        TrayNotofier.Icon = new System.Drawing.Icon(System.IO.Directory.GetCurrentDirectory() + @"/images/notofy1_pbm_pp_bt.ico");
                        break;

                }
            }
            catch (Exception ex)
            {
                
                throw new Exception(ex.Message);
            }
        }
        [Flags()]
        public enum NotificationFlags
        {
            IDLE,
            CONNECTION,
            LOSTCONNECTION,
            BLUETOOTH
        }



        public void ShowNotfivationIcon(NotifyIcon notifyIcon)
        {
            TrayNotofier = notifyIcon;
            this.ShowNotfivationIcon();
        }
    }
}
